import {
  useEffect,
  useState,
} from 'react'
import {
  useThrottleFn,
  type ThrottleOptions,
} from './use-throttle-fn'

export function useThrottle<T>(value: T, options?: ThrottleOptions): T {
  const [throttled, setThrottled] = useState(value)

  const { run } = useThrottleFn(() => {
    setThrottled(value)
  }, options)

  useEffect(() => {
    run()
  }, [value])

  return throttled
}
